package com.example.leetcode;

public class SearchInsert {
    public static void main(String[] args) {
        int[] nums = {1, 3, 5, 6};
        int target = 2;
        System.out.println(searchInsert(nums, target));
    }

    public static int searchInsert(int[] nums, int target) {
        int start = 0;
        int end = nums.length - 1;
        int mind = 0;
        while (start <= end) {
            mind = (end + start) / 2;
            int num = nums[mind];
            if (num == target) {
                return mind;
            } else if (num < target) {
                start = mind + 1;
            } else if (num > target) {
                end = mind - 1;
            }
        }
        if (nums[mind] < target) {
            return mind + 1;
        } else {
            return mind - 1;
        }
    }
}
